package com.example.basestationforballonproject.utils;

import com.example.basestationforballonproject.exceptions.StringIsNotAPhoneNumberException;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.text.TextUtils;

public class SmsSender {

	private SmsManager smsManager = SmsManager.getDefault();
	private PendingIntent pendingIntentSent, pendingIntentDelivered;
	
	public SmsSender(Context context) {
		pendingIntentSent = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
		pendingIntentDelivered = PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
	}

	public void sendSMS(String phoneNumber, String message) throws StringIsNotAPhoneNumberException {
		
		if(isValidPhoneNumber(phoneNumber))	{
			smsManager.sendTextMessage(phoneNumber, null, message, pendingIntentSent, pendingIntentDelivered);
			
		} else {
			throw new StringIsNotAPhoneNumberException();
		}
	}
	
	public static boolean isValidPhoneNumber(CharSequence number) {
	    if (number == null || TextUtils.isEmpty(number)) {
	        return false;
	    } else {
	        return android.util.Patterns.PHONE.matcher(number).matches();
	    }
	}
}
